import {getStore, setStore} from "@/config/util";

/**
 * 重载页面
 * 小程序无法直接刷新，将重新执行页面的onLoad,onShow,mounted
 * 确保这三个生命周期中是幂等操作
 */
export function reloadCurrentPage() {
    /* #ifdef H5 */
    location.reload();
    /* #endif */

    /* #ifdef MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO */
    let pages = getCurrentPages();
    let current = pages[pages.length - 1];
    if (current) {
        current.mounted && current.mounted(current.options);
        current.onLoad && current.onLoad(current.options);
        current.onShow && current.onShow();
        // 页面级mounted不会主动触发子组件mounted，手动调用一下
        callLifecycle(current.$vm.$children, 'mounted');
    }
    /* #endif */
}


export const getToken = () => {
    let token = getStore('pb-token', true);
    return token ? 'Bearer ' + token : '';
};

export const setToken = (token) => {
    setStore('pb-token', token, true);
};
